package SoftwarePrinciple.LawOfDemeter;

/**
 * @author: Themberfue
 * @date: 2025/8/8 11:09
 * @description:
 */
// 书籍类
class Book {
    private String title;
    private String author;
    private String isbn;
    private boolean isAvailable;

    public Book(String title, String author, String isbn) {
        this.title = title;
        this.author = author;
        this.isbn = isbn;
        this.isAvailable = true; // 新书默认可借阅
    }

    // 书籍内部状态变更
    public void borrowBook() {
        if (isAvailable) {
            isAvailable = false;
        }
    }

    public void returnBook() {
        if (!isAvailable) {
            isAvailable = true;
        }
    }

    // 提供必要的查询方法，隐藏内部状态
    public boolean isAvailable() {
        return isAvailable;
    }

    public String getTitle() {
        return title;
    }

    public String getIsbn() {
        return isbn;
    }
}

// 书架类
class Bookshelf {
    private String shelfId;
    private Book[] books; // 直接管理的书籍（朋友关系）

    public Bookshelf(String shelfId, Book[] books) {
        this.shelfId = shelfId;
        this.books = books;
    }

    // 提供查询书籍的方法，隐藏内部存储细节
    public Book findBookByIsbn(String isbn) {
        for (Book book : books) {
            if (book.getIsbn().equals(isbn)) {
                return book;
            }
        }
        return null;
    }

    // 提供检查书籍是否可借的方法，避免外部直接操作Book
    public boolean isBookAvailable(String isbn) {
        Book book = findBookByIsbn(isbn);
        return book != null && book.isAvailable();
    }
}

// 图书馆类
class Library {
    private String name;
    private Bookshelf[] bookshelves; // 直接管理的书架（朋友关系）

    public Library(String name, Bookshelf[] bookshelves) {
        this.name = name;
        this.bookshelves = bookshelves;
    }

    // 借书流程：只与直接朋友Bookshelf交互
    public boolean borrowBook(String isbn) {
        // 查找书籍所在的书架
        for (Bookshelf shelf : bookshelves) {
            Book book = shelf.findBookByIsbn(isbn);
            if (book != null && book.isAvailable()) {
                book.borrowBook();
                System.out.println("成功借出书籍: " + book.getTitle());
                return true;
            }
        }
        System.out.println("无法借出书籍，ISBN: " + isbn);
        return false;
    }

    // 还书流程：只与直接朋友Bookshelf交互
    public boolean returnBook(String isbn) {
        for (Bookshelf shelf : bookshelves) {
            Book book = shelf.findBookByIsbn(isbn);
            if (book != null && !book.isAvailable()) {
                book.returnBook();
                System.out.println("成功归还书籍: " + book.getTitle());
                return true;
            }
        }
        System.out.println("无法归还书籍，ISBN: " + isbn);
        return false;
    }

    // 检查书籍是否可借：只暴露必要接口
    public boolean isBookAvailable(String isbn) {
        for (Bookshelf shelf : bookshelves) {
            if (shelf.isBookAvailable(isbn)) {
                return true;
            }
        }
        return false;
    }
}

// 读者类
class Reader {
    private String name;
    private String readerId;

    public Reader(String name, String readerId) {
        this.name = name;
        this.readerId = readerId;
    }

    // 借书操作：只与Library交互（直接朋友）
    public void borrowFromLibrary(Library library, String isbn) {
        System.out.println("\n读者 " + name + " 尝试借书，ISBN: " + isbn);
        if (library.isBookAvailable(isbn)) {
            library.borrowBook(isbn);
        } else {
            System.out.println("书籍不可借或不存在");
        }
    }

    // 还书操作：只与Library交互（直接朋友）
    public void returnToLibrary(Library library, String isbn) {
        System.out.println("\n读者 " + name + " 尝试还书，ISBN: " + isbn);
        library.returnBook(isbn);
    }
}

// 主程序
public class LibrarySystem {
    public static void main(String[] args) {
        // 创建书籍
        Book book1 = new Book("Java编程思想", "Bruce Eckel", "9787111213826");
        Book book2 = new Book("设计模式", "Erich Gamma", "9787111407010");
        Book book3 = new Book("数据结构与算法分析", "Mark Allen Weiss", "9787115449374");

        // 创建书架
        Bookshelf shelf1 = new Bookshelf("S001", new Book[]{book1, book2});
        Bookshelf shelf2 = new Bookshelf("S002", new Book[]{book3});

        // 创建图书馆
        Library library = new Library("科技图书馆", new Bookshelf[]{shelf1, shelf2});

        // 创建读者
        Reader reader = new Reader("李明", "R001");

        // 执行借书还书操作
        reader.borrowFromLibrary(library, "9787111213826"); // 借《Java编程思想》
        reader.borrowFromLibrary(library, "9787111213826"); // 再次借同一本书（应该失败）
        reader.returnToLibrary(library, "9787111213826");   // 还书
        reader.borrowFromLibrary(library, "9787111213826"); // 再次借书（应该成功）
    }
}

